<template>
  <el-scrollbar class="con">
    <div
      class="box"
      v-if="dataList.length > 0"
      v-infinite-scroll="scrollLoad"
      :infinite-scroll-disabled="disabled"
      infinite-scroll-distance="80">
      <div class="list" @click="goDetail(item, index)" v-for="(item, index) in dataList" :key="item.id">
        <div class="title">{{ item.noticeName }}</div>
        <div class="info">
          <div class="type">{{ item.busSign }}</div>
          <div class="time">{{ item.createTime }}</div>
        </div>
        <div class="read" v-if="item.isRead == 0"></div>
      </div>
      <!-- <div class="list">
        <div class="title">2021年4月份工作纪实</div>
        <div class="info">
          <div class="type">用户通知</div>
          <div class="time">2021.02.14</div>
        </div>
      </div> -->
    </div>

    <template v-if="dataList.length > 0">
      <p v-if="sysLoading" class="info-tips">加载中...</p>
      <p v-if="noMore" class="info-tips">没有更多了</p>
    </template>
    <empty v-if="dataList.length == 0"></empty>
  </el-scrollbar>
</template>

<script>
import { listNoticesByTypeTop, updateNoticeRead } from '@apis/message.js'
export default {
  name: 'MessageList',
  components: {},
  props: {
    messageType: {
      type: String,
    },
  },
  data() {
    return {
      // 消息类型对应关系
      msgList: {
        NOTE_NOTICE: '01',
      },
      dataList: [],
      pageInfo: {
        currentPage: 1,
        pageSize: 20,
      },
      sysLoading: false,
      noMore: false,
      sysListTotal: 0,
    }
  },
  computed: {
    disabled() {
      return this.sysLoading || this.noMore
    },
  },
  watch: {
    messageType() {
      this.dataList = []
      this.pageInfo.currentPage = 1
      this.getPageListData()
    },
  },
  created() {},
  mounted() {},
  methods: {
    goDetail({ url, id, isRead }, index) {
      if (isRead === '0') {
        updateNoticeRead({
          id,
        }).then(res => {
          if (res.code === 0) {
            if (url) {
              if (this.messageType === '01') {
                this.goDoc(url)
              } else if (this.messageType === 'rckq') {
                this.goAttendance(url)
              } else if (this.messageType === 'PEOPLE_ASSESS') {
                this.goMinZhu(url)
              } else if (this.messageType === 'ORG_MODIFY_RANK') {
                this.goGrade(url)
              } else {
                this.$router.push({
                  path: url,
                })
              }
            } else {
              this.dataList[index].isRead = '1'
              this.$emit('changeState', this.messageType)
            }
          }
        })
      } else {
        if (this.messageType === '01') {
          this.goDoc(url)
        } else if (this.messageType === 'rckq') {
          this.goAttendance(url)
        } else if (this.messageType === 'PEOPLE_ASSESS') {
          this.goMinZhu(url)
        } else if (this.messageType === 'ORG_MODIFY_RANK') {
          this.goGrade(url)
        } else {
          this.$router.push({
            path: url,
          })
        }
      }
    },
    async getPageListData() {
      this.sysLoading = true
      const { status, data } = await listNoticesByTypeTop({
        ...this.pageInfo,
        param: {
          /**
           * '': 所有的
           * 1: 纪实
           * 2:评价
           */
          isRead: '',
          noticeType: this.messageType,
        },
      })
      if (status === 200) {
        this.dataList = this.dataList.concat(data.content || [])

        this.sysListTotal = data.totalSize
        this.pageInfo.currentPage++
        this.sysLoading = false
        if (this.dataList.length === this.sysListTotal) {
          this.noMore = true
        }
      }
    },

    scrollLoad() {
      if (!this.sysLoading) {
        this.getPageListData()
      }
    },
    goDoc(url) {
      const urlArr = url.split('?')
      const str = urlArr[0]
      const urlStr = url.replace(str, '/document/index-detail') + '&isEnd=true'
      this.$router.push(urlStr)
    },
    goMinZhu(url) {
      const urlArr = url.split('?')
      const str = urlArr[0]
      const urlStr = url.replace(str, '/minzhu/create') + '&isEnd=true'
      this.$router.push(urlStr)
    },
    goAttendance(url) {
      const urlArr = url.split('?')
      const str = urlArr[0]
      const urlStr = url.replace(str, '/attendance/check') + '&isEnd=true'
      this.$router.push(urlStr)
    },
    goGrade(url) {
      const urlArr = url.split('?')
      const str = urlArr[0]
      const urlStr = url.replace(str, '/grade-manage')
      this.$router.push(urlStr)
    },
  },
}
</script>

<style lang="scss" scoped>
.con {
  flex: 1;
  height: 100%;
  .box {
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-content: flex-start;
    flex-wrap: wrap;
    .list {
      width: 48%;
      padding: 15px;
      border: 1px solid #f0e7d8;
      margin-bottom: 15px;
      cursor: pointer;
      position: relative;
      &:hover {
        box-shadow: 2px 4px 12px 1px rgba(191, 154, 118, 0.2);
        border: solid 1px #bf9a76;
      }
      .title {
        font-weight: 600;
        color: #333;
        font-size: 16px;
        margin-bottom: 10px;
      }
      .info {
        display: flex;
        align-items: center;
        font-size: 12px;
        .type {
          color: #bf9a76;
          padding-right: 15px;
        }
        .time {
          padding-left: 15px;
          border-left: 1px solid #ccc;
        }
      }

      .read {
        position: absolute;
        top: calc(50% - 5px);
        right: 10px;
        background: #ff0000;
        height: 10px;
        width: 10px;
        border-radius: 10px;
      }
    }
  }
  .info-tips {
    text-align: center;
    font-size: 14px;
    color: #666;
    margin-bottom: 10px;
  }
}
</style>
